package org.spring.springboot.app.order.harddeviceorder.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.order.harddeviceorder.dao.DeviceOrderDao;
import org.spring.springboot.app.order.harddeviceorder.domain.po.DeviceOrderPO;
import org.spring.springboot.app.order.harddeviceorder.domain.vo.DeviceOrderSearchVO;
import org.spring.springboot.app.order.harddeviceorder.domain.vo.DeviceOrderVO;
import org.spring.springboot.app.order.harddeviceorder.service.IDeviceorderService;
import org.spring.springboot.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class DeviceorderServiceImpl implements IDeviceorderService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceorderServiceImpl.class);

    @Autowired
    private DeviceOrderDao deviceorderDao;

    // key生成器,redis生成key必须用
    @Autowired
    private RedisUtils redisUtils;

    @Value("${file.img}")
    private String fileImg;

    public void removeSysDictCache() {
        redisUtils.delete("notice");
    }

    @Override
    public DeviceOrderPO find(String id) {
        return deviceorderDao.selectByPrimaryKey(id);
    }

    @Override
    public List<DeviceOrderVO> findPage(DeviceOrderSearchVO searchvo, int pageNum, int pageSize) {
        List<DeviceOrderVO> list=deviceorderDao.findPage(searchvo,pageNum, pageSize);
        for(int i=0;i<list.size();i++){
            String img="";
            ObjectMapper objectMapper=new ObjectMapper();
            List<Map> list3=null;
            //转化为list
            if(list.get(i).getDeviceImage()!=null){
                try{
                    list3= objectMapper.readValue(list.get(i).getDeviceImage(), List.class);

                }catch(Exception e){
                    e.printStackTrace();
                }

                for(int j=0;j<list3.size();j++){
                    img+=fileImg+list3.get(j).get("path")+list3.get(j).get("id")+"."+list3.get(j).get("suffix")+"?process=image/resize,width_"+list3.get(j).get("width")+",height_"+list3.get(j).get("height");
                    list.get(i).setDeviceImage(img);
                }
            }
        }
        return list;
    }

    @Override
    public int save(DeviceOrderPO po) {
        po.preInsert();
        int i = deviceorderDao.insertSelective(po);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }

    @Override
    public int update(DeviceOrderPO po) {
        po.preUpdate();
        int i = deviceorderDao.updateByPrimaryKeySelective(po);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }

    @Override
    public int delete(String id) {
        DeviceOrderPO deviceorderPO = new DeviceOrderPO();
        deviceorderPO.setId(id);
        deviceorderPO.preDelete();
        int i = deviceorderDao.updateByPrimaryKeySelective(deviceorderPO);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }

    @Override
    public int deletes(String ids) {
        DeviceOrderPO deviceorderPO = new DeviceOrderPO();
        String[] id_s = ids.split(",");
        deviceorderPO.preDelete();
        int count = 0;
        for (String id : id_s) {
            deviceorderPO.setId(id);
            count += deviceorderDao.updateByPrimaryKeySelective(deviceorderPO);
        }
        return count;
    }

}
